package com.edgar.paysdk

import android.content.Context

/**
 * Created by edgar on 18-1-21.
 */
class PaySdk private constructor(){

    companion object {
        private val mPayMap:MutableMap<PayType, PayPlatform> = HashMap()
        private @JvmStatic val SDK_PACKAGE_NAME = "com.edgar.paysdk"
//        private @JvmStatic var sInstance = lazy<PaySdk>({ PaySdk() })
//        @JvmStatic fun getInstance():PaySdk = sInstance.value

        @JvmStatic fun <Pay:PayPlatform> getPay(payType: PayType): Pay {
            synchronized(mPayMap){
                var pay = mPayMap[payType]
                if (pay == null) {
                    val payClass = generateClassWitchType(payType)
                    val constructor = payClass.getConstructor(Context::class.java)
                    pay = constructor.newInstance(PayInitialize.getContext()) as PayPlatform
                    mPayMap.put(payType,pay)
                }
                return pay as Pay
            }
        }

        @JvmStatic fun initialize(context: Context){
            PayInitialize.initialize(context)
        }

        @JvmStatic fun setWeChatAppId(weChatAppId: String){
            PayInitialize.setWeChatAppId(weChatAppId)
        }

        private fun generateClassWitchType(payType:PayType):Class<*> {
            val name =payType.name
            val className = name+"Pay"
            return Class.forName(SDK_PACKAGE_NAME+"."+name.toLowerCase()+"."+className) as Class<*>
        }
    }
}